/*   
 * Copyright 2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.beaconframework.ext.rs;

import java.util.Map;

import javax.ws.rs.core.Response;

import org.beaconframework.vaadin.data.QueryTemplate;
import org.beaconframework.vaadin.data.XmlItemContainer;
import org.beaconframework.vaadin.ui.DynamicFilter;
import org.w3c.dom.Document;

public class RestXmlItemContainer extends XmlItemContainer implements
        DynamicFilter {

    private static final long serialVersionUID = -3484487073102682219L;
    private QueryTemplate<? extends Object> queryTemplate = new DummmyQueryTemplate();

    private String lastFilter = null;

    public class DummmyQueryTemplate extends QueryTemplate<Object> {

        public Response doExecuteQuery(Map<String, Object> arg)
                throws Exception {
            // TODO Auto-generated method stub
            return null;
        }


        public Object doHandleException(Exception t) throws Exception {
            // TODO Auto-generated method stub
            return null;
        }


        public Object doProcessResult(Object result) throws Exception {
            // TODO Auto-generated method stub
            return null;
        }

    }


    public void setQueryTemplate(QueryTemplate<? extends Object> queryTemplate) {
        this.queryTemplate = queryTemplate;
    }


    public QueryTemplate<? extends Object> getQueryTemplate() {
        return queryTemplate;
    }


    public final Document executeQuery(Object arg) {
        Document d = (Document) getQueryTemplate().query(arg);

        return d;
    }


    public void queryCriteriaChanged(String filter) {
        removeAllItems();
        addDocument((Document) getQueryTemplate().query(filter));

    }


    public void refresh() {
        queryCriteriaChanged(lastFilter);
    }
}
